草庐IT

C++ this指针总是const

全部标签

Javascript 'this'

你能解释一下为什么第二次调用fn会出错吗?代码如下。functionTest(n){this.test=n;varbob=function(n){this.test=n;};this.fn=function(n){bob(n);console.log(this.test);};}vartest=newTest(5);test.fn(1);//returns5test.fn(2);//returnsTypeError:'undefined'isnotafunction这是一个重现错误http://jsfiddle.net/KjkQ2/的JSfiddle 最佳答

javascript - Node.js Q promise ,可以使用 this() 为什么要使用 defer()?

我想做这样的事情:somePromiseFunc(value1).then(function(value2,callback){//insertthenextthen()intothisfunction:funcWithCallback(callback);}).then(function(dronesYouAreLookingFor){//Haveaparty}).done();它没有用。我无法让它工作。我被建议为此目的使用defer()。他们的owndocs说我们应该将deferreds用于回调式函数。虽然这令人困惑,因为他们著名的压平金字塔示例都是关于回调的,但是这个示例太抽象了

javascript - 初学者的 : const definition in Redux confusing

在这个Redux入门类(class)中https://egghead.io/lessons/javascript-redux-store-methods-getstate-dispatch-and-subscribe?series=getting-started-with-redux,主持人说下面两行是一样的const{createStore}=Redux;varcreateStore=Redux.createStore;我刚刚搜索了ES6const文档,它并没有完全回答我的问题,这两行如何相同? 最佳答案 这与const(这只是定

javascript - 使用 event.target.id 从 bind(this) 获取 id 时意外使用 'event' no-restricted-globals

此代码适用于Codepen:参见https://codepen.io/pkshreeman/pen/YQNPKB?editors=0010然而,我试图在我自己的“create-react-app”中使用它,并且“no-restricted-globals”的错误是由event.target.id触发的。有什么解决方法。除了使用事件目标之外,您如何从“this”中获取id?constElem=(props)=>{return(GoodMorning!{props.name}{props.last}Thisisphasethree{props.text}SecondButton);};cl

javascript - 为什么 element.style 在 JS 中总是返回空?

当您在css中定义display:block时,element.style.display总是返回空的。console.log(document.getElementById('test').style.display)#map{display:block;}test但是如果你在该元素中设置样式,那么我们可以获得样式。显示详细信息。console.log(document.getElementById('test').style.display)test我不想要解决方案,因为我知道有很多解决方案:getElementById().style.displaydoesnotworkShow

javascript - 我的 'this' 在哪里?使用对象方法作为回调函数

我有一个关于javascript规范或函数指针(委托(delegate)?)实现的一般性问题,它们指向对象方法。请阅读以下代码片段。这里我们有一个对象,其方法使用“this”来访问对象字段。当我们像往常一样调用此方法时(o.method();),返回对象指定字段的值。但是当我们创建指向此方法(回调)的指针并调用它时,返回一个未定义的值,因为方法范围内的“this”现在是全局对象。varo={field:'value',method:function(){returnthis.field;}};o.method();//returns'value'varcallback=o.method

javascript - 我什么时候应该使用 `this.x` 与 `var x` ?

在创建JavaScript类函数时,我使用了this。很多。但是在使用它时,我想知道使用var是否会有所不同。varMyClass=function(){this.x=4;return{getVal:this.x};}与var的使用对比:varMyClass=function(){varx=4;return{getVal:x};}有什么区别,什么时候应该使用哪个?同样的问题适用于class语法:classMyClass{constructor(){constx=4;}}对比classMyClass{constructor(){this.x=4;}} 最佳答案

javascript - 为什么 (function() { return this; }).call ('string literal' ) 返回 [String : 'string literal' ] instead of 'string literal' ?

这是我在试验JS时的最新发现:(function(){returnthis;}).call('stringliteral');//=>[String:'stringliteral']inV8//=>String{"stringliteral"}inFF我在执行以下操作时偶然发现了这一点:(function(){returnthis==='stringliteral';}).call('stringliteral');//=>false谁能告诉我为什么函数内部的this不是作为第一个参数传递给call的正是?编辑1Whatisthedifferencebetweenstringprimi

javascript - 使用 goog.bind 和 goog.net.Xhrio.send 了解 "this"上下文

我对调用以下代码时发生的情况感到有点困惑:goog.net.XhrIo.send("/welcome",goog.bind(this.handleWelcome,this));我有一个带有这个签名的函数:myproject.MyClass.prototype.handleWelcome=function(response)在绑定(bind)之前,handleWelcome的上下文无法访问我的Javascript类myproject.MyClass的实例字段(这是可以理解的)。关注信息here,我现在有了类实例的上下文。一切都很好。在我进行更改之前,“this”的上下文是什么?请原谅我使

javascript - 将 "this"用作函数?

我继承了一些我不理解的代码。functionupdateQty(){obj.find('.inputAmount').html(qty);input.val(qty);$.each(change_actions,function(){this(qty);});}.each函数内部到底发生了什么?我以前从未见过this(var)以这种方式使用过。 最佳答案 $.each中的this指的是您正在循环的当前对象。对象必须是一个函数才能向它传递一些东西。 关于javascript-将"this"